package com.samoilovich.belarustoday.afisha.films;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.samoilovich.belarustoday.Constants;
import com.samoilovich.belarustoday.basefragments.BaseFragment;
import com.samoilovich.belarustoday.R;
import com.samoilovich.belarustoday.adapters.SeancesAdapter;
import com.samoilovich.belarustoday.afisha.AfishaActivity;
import com.samoilovich.belarustoday.calendar.fragments.CalendarHelper;
import com.samoilovich.belarustoday.calendar.fragments.ICalendarListener;
import com.samoilovich.belarustoday.datemodel.CinemaModel;
import com.samoilovich.belarustoday.datemodel.FilmModel;
import com.samoilovich.belarustoday.datemodel.SeanceModel;
import com.samoilovich.belarustoday.handlers.AfishaContentHandler;
import com.samoilovich.belarustoday.tools.Utility;

import java.util.ArrayList;
import java.util.Date;

import hirondelle.date4j.DateTime;

public class SeancesFragment extends BaseFragment implements ICalendarListener{
    private String TAG = getClass().getSimpleName();
    private final String NULL = "null";
    private static ArrayList<SeanceModel> sSeances;
    private static FilmModel mCurrentFilm;
    private RelativeLayout mSeancesContent;
    private SeancesAdapter mAdapter;
    private String mDate;
    private ProgressBar mProgressBarContentWait;
    private View mViewNoConnection;
    private ListView mListView;
    private View.OnClickListener mCinemaClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int position = (Integer) view.getTag();
            CinemaModel cinema = mAdapter.getItem(position).getCinema();
            if ((!cinema.getLatitude().equals(NULL)) && (!cinema.getLongitude().equals(NULL))) {
                ((AfishaActivity) getActivity()).setCinemaFragment(cinema);
            }
        }
    };

    public SeancesFragment() {
    }

    public SeancesFragment(Context context, FilmModel film, String date) {
        mInProgress = false;
        mDate = date;
        mCurrentFilm = film;
        getSeancesList(context);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_seances, null);
        prepareActionBarViews(R.string.seances, R.string.films, true);
        prepareViews();

        return mRootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (sSeances != null) {
            updateScreen();
        }
    }

    public void updateSeancesList(final ArrayList<SeanceModel> seances) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                sSeances = seances;
                updateScreen();
                mInProgress = false;
            }
        });
    }

    @Override
    public void onReceiveInternetIntent(boolean hasInternetConnection, Intent intent) {
        if (mListView == null || mProgressBarContentWait == null || mProgressBarContentWait == null || mAdapter == null) {
            prepareViews();
        }
        if (hasInternetConnection) {
            mViewNoConnection.setVisibility(View.GONE);
            if (!mInProgress && sSeances == null) {
                getSeancesList(getActivity());
            }
        } else {
            if (sSeances == null) {
                mViewNoConnection.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void updateAllData(Context context) {
        updateSeancesList();
        getSeancesList(context);
    }

    @Override
    public void onSelectDate(Date date, View view) {
        ((AfishaActivity) getActivity()).dismissCalendarDialog();
        mDate = CalendarHelper.getDateAsString(date);
        updateAllData(getActivity());
    }

    @Override
    public void onLongClickDate(Date date, View view) {
    }

    @Override
    public void onChangeMonth(int month, int year) {
    }

    @Override
    public void onCalendarViewCreated() {
    }

    // -----------------------------------------------------------------------------------
    // -----------------------------------------------------------------------------------
    private void prepareViews() {
        mProgressBarContentWait = (ProgressBar) mRootView.findViewById(R.id.progressBar_categoryWait);
        mViewNoConnection = mRootView.findViewById(R.id.view_noConnection);
        mSeancesContent = (RelativeLayout) mRootView.findViewById(R.id.relativeLayout_seancesContent);
        mListView = (ListView) mRootView.findViewById(R.id.listView_category);
        mAdapter = new SeancesAdapter(getActivity(), null, R.layout.seance_list_item, mCinemaClickListener);
        mListView.setAdapter(mAdapter);
        if (mCurrentFilm != null) {
            ((TextView) mRootView.findViewById(R.id.textView_name)).setText(mCurrentFilm.getName());
            ((TextView) mRootView.findViewById(R.id.textView_year)).setText(mCurrentFilm.getYear());
        }

        Button calendarButton = (Button) mRootView.findViewById(R.id.button_calendar);
        calendarButton.setVisibility(View.VISIBLE);
        calendarButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DateTime fromDate, toDate;
                if (mDate == null) {
                    mDate = CalendarHelper.getTodayAsString();
                }
                DateTime clickedDate = CalendarHelper.getDateTimeFromString(mDate, null);
                ((AfishaActivity) getActivity()).setCalendarSeancesFragment(SeancesFragment.this, mCurrentFilm.getDayList(), clickedDate);
            }
        });
    }

    private void updateScreen() {
        if (mSeancesContent == null || mProgressBarContentWait == null || mAdapter == null) {
            prepareViews();
        }
        mSeancesContent.setVisibility(View.VISIBLE);
        mProgressBarContentWait.setVisibility(View.GONE);
        mAdapter.setFilms(sSeances);
    }

    private void updateSeancesList() {
        if (mSeancesContent == null || mProgressBarContentWait == null || mAdapter == null) {
            prepareViews();
        }
        mSeancesContent.setVisibility(View.GONE);
        mViewNoConnection.setVisibility(View.GONE);
        mProgressBarContentWait.setVisibility(View.VISIBLE);
    }

    private void getSeancesList(Context context) {
        sSeances = null;
        if (Utility.hasInternetConnection(context)) {
            mInProgress = true;
            if (mDate == null) {
                mDate = CalendarHelper.getTodayAsString();
            }
            AfishaContentHandler.getInstance().getSeancesList(this, mCurrentFilm.getId(), mDate);
        }
    }

}
